import * as constants from './constants'

const _getLogin = (data) => ({
  type: constants.LOGIN_STATUS,
  payload: data
})
const _login = () => ({
  type: constants.LOGIN,
  payload: true
})
const _logout = () => ({
  type: constants.LOGOUT,
  payload: false
})

export const getLogin = () => {
  return (dispatch, getState, instanceAxios) => {
    return instanceAxios.get('/api/isLogin.json?secret=M5s2sPneDE')//由node进行一次代理
      .then(res => {
        if (res.status === 200) {
          dispatch(_getLogin(res.data.data.login))
        }
      }).catch(err => {
        console.log('请求失败', err)
      })
  }
}
export const login = () => {
  return (dispatch, getState, instanceAxios) => {
    return instanceAxios.get('/api/login.json?secret=M5s2sPneDE')//由node进行一次代理
      .then(res => {
        if (res.status === 200) {
          dispatch(_login())
        }
      }).catch(err => {
        console.log('请求失败', err)
      })
  }
}
export const logout = () => {
  return (dispatch, getState, instanceAxios) => {
    return instanceAxios.get('/api/logout.json?secret=M5s2sPneDE')//由node进行一次代理
      .then(res => {
        if (res.status === 200) {
          dispatch(_logout())
        }
      }).catch(err => {
        console.log('请求失败', err)
      })
  }
}